@extends('base')

@section('title','目录')

@section('content')

    {{csrf_field()}}

    <div  class="weui_cells weui_cells_select">
        <div class="weui_cell weui_cell_select" >
            <div class="weui_cell_bd weui_cell_primary">
                <select id="sel" class="weui_select"  onchange="sendParentId(this)">
                    @foreach($parentCates as $parentCate)
                        <option value="{{$parentCate->CateId}}">{{$parentCate->CateName}}</option>
                    @endforeach
                </select>
            </div>
        </div>
    </div>
    <div id='select' ></div>

@endsection


@section('js')

    <script type="text/javascript">

        $('document').ready(function () {
            var postData = {
                'parentId':$('#sel').val(),
                '_token':$("input[name='_token']").val()
            };

            $.post("{{url('Service/Category/getCategories')}}",postData,function (res) {
                var childCateNames = res.data;
                //console.log(res.data);
                var url = "{{url('Service/Product/getProducts')}}/";
                $('a').remove();
                for(var i=0;childCateNames.length;i++){
                    var node ='<a class="weui_cell" href="'+url+childCateNames[i].CateId+'">'+
                        '<div class="weui_cell_bd weui_cell_primary">'+
                        childCateNames[i].CateName+
                        '</div>'+
                        '</a>';
                    $('#select').append(node);
                }
            },'JSON')
        });

        function sendParentId(obj) {
            var postData = {
                'parentId':obj.value,
                '_token':$("input[name='_token']").val()
            };

            $.post("{{url('Service/Category/getCategories')}}",postData,function (res) {
                var childCateNames = res.data;
                //console.log(res.data);
                var url = "{{url('Service/Product/getProducts')}}/";
                $('a').remove();
                for(var i=0;childCateNames.length;i++){
                    var node ='<a class="weui_cell" href="'+url+childCateNames[i].CateId+'">'+
                                    '<div class="weui_cell_bd weui_cell_primary">'+
                                        childCateNames[i].CateName+
                                    '</div>'+
                                '</a>';
                    $('#select').append(node);
                }
            },'JSON')
        }
    </script>

@endsection